﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Modelling;
using ITAP.ShprossesBuilder.Editor;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.ShprossesBuilder.Properties;
using ITAP.Core.Modelling;
using ITAP.Core.Calculations;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationGetDistance : IOperation
    {
        #region IOperation Members

        public string Name
        {
            get { return "Расстояние между элементами"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.Other; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.Distance.ToBitmap(); }
        }

        public void Execute(ModelControl modelControl)
        {
            ModelPoint point1 = null;
            ModelPoint point2 = null;
            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
            {
                if (selectedComponent is ShprossConnector)
                {
                    if (point1 == null)
                    {
                        point1 = selectedComponent.Position;
                        continue;
                    }
                    if (point2 == null)
                    {
                        point2 = selectedComponent.Position;
                        continue;
                    }
                }
                else if (selectedComponent is Shpross)
                {
                    if (point1 == null)
                    {
                        point1 = (selectedComponent as Shpross).SelectedPointPosition;
                        continue;
                    }
                    if (point2 == null)
                    {
                        point2 = (selectedComponent as Shpross).SelectedPointPosition;
                        continue;
                    }
                }
            }
            double distance = ModelCalculations.GetDistance(point1, point2);

            DistanceForm distanceForm = new DistanceForm(distance);
            distanceForm.ShowDialog();
        }

        public bool CanUndo
        {
            get { return false; }
        }

        #endregion
    }
}
